# coding=utf-8

import sys, os  
reload(sys)
#sys.setdefaultencoding('utf-8') 
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner import MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.models import ViewNode

sys.path.append(os.path.split(os.path.realpath(__file__))[0])
import util

#

def resetLocation(device, easy_device, lat, lon, imgMap):
	util.printLog("Now reset location with " + "lat: " +lat + "lon: " + lon)
	device.shell('am start -a android.intent.action.MAIN -n com.tim.apps.mockgps/.TestGPSActivity')
	MonkeyRunner.sleep(6)
	task_high = 50
	
	#删除已收藏位置
	util.press(device, 'KEYCODE_MENU')
	MonkeyRunner.sleep(1)
	util.press(device, 'KEYCODE_DPAD_RIGHT')
	util.press(device, 'KEYCODE_DPAD_CENTER')
	util.pressLong(device)
	util.press(device, 'KEYCODE_DPAD_DOWN')
	util.press(device, 'KEYCODE_DPAD_CENTER')
	util.press(device, 'KEYCODE_BACK')
	
	#收藏位置 
	util.press(device, 'KEYCODE_MENU')
	MonkeyRunner.sleep(1)
	util.press(device, 'KEYCODE_DPAD_RIGHT')
	util.press(device, 'KEYCODE_DPAD_CENTER')
	util.type(device, 'resetLocation')
	util.press(device, 'KEYCODE_DPAD_DOWN')
	util.clearEditText(device)
	util.type(device, lat)
	util.press(device, 'KEYCODE_DPAD_DOWN')
	util.clearEditText(device)
	util.type(device, lon)
	util.press(device, 'KEYCODE_DPAD_DOWN')
	util.press(device, 'KEYCODE_DPAD_LEFT')
	util.press(device, 'KEYCODE_DPAD_CENTER')
	
	#设置收藏后的位置
	util.press(device, 'KEYCODE_MENU')
	util.press(device, 'KEYCODE_DPAD_CENTER')
	MonkeyRunner.sleep(2)
	util.pressByIdRelative(device, "id/listViewFavs", task_high, 0,1)
	MonkeyRunner.sleep(7)
	easy_device.touch(By.id('id/btnSet'), 'downAndUp')
	MonkeyRunner.sleep(3)
	util.press(device, 'KEYCODE_HOME')
	MonkeyRunner.sleep(2)

def main():
	imgMap = {}
	device = MonkeyRunner.waitForConnection()
	easy_device = EasyMonkeyDevice(device)
	resetLocation(device, easy_device, "29.111111", "116.222222", imgMap)

if __name__ == '__main__':
	main()